﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Clandestine.Battle.Effects
{
    public class Fire : BattleEffect
    {
        private int duration = 800;

        public override void Action(Combatant source, Combatant[] targets)
        {
            // Todo: Inefficient rly as we're loading the fire for each target.. might make an AnimatedSprite.Clone 
            AnimatedSprite[] sprites = new AnimatedSprite[targets.Length];
            for (int i = 0; i < targets.Length; i++)
            {
                sprites[i] = new AnimatedSprite("fire.png", 48, 78, new SpritesheetSelection[] {
                    new SpritesheetSelection("fire", 0, 0, 0, 7)
                });
                sprites[i].Position = new System.Drawing.Point(targets[i].Sprite.X + (targets[i].Sprite.UnscaledWidth / 2) - (sprites[i].UnscaledWidth / 2), targets[i].Sprite.Y - (sprites[i].UnscaledHeight - targets[i].Sprite.UnscaledHeight));
                sprites[i].Layer = targets[i].Sprite.Layer + 0.1f;
                sprites[i].Scale = 1;
                sprites[i].Color.A = 0.5f;
                sprites[i].CurrentAnimation.Interval = duration / sprites[i].CurrentAnimation.FrameCount;
                sprites[i].Visible = false;
            }
            AnimatedSprite sourcePowa = new AnimatedSprite("fire.png", 48, 78, new SpritesheetSelection[] {
                    new SpritesheetSelection("fire", 0, 0, 0, 7)
                });
            sourcePowa.CurrentAnimation.Interval = duration / sourcePowa.CurrentAnimation.FrameCount;
            sourcePowa.Visible = true;
            sourcePowa.Position = source.Sprite.Position;
            sourcePowa.Playing = true;
            sourcePowa.Layer = -0.5f;
            sourcePowa.Scale = 2f;
            sourcePowa.IgnoreGlobalColor = true;

            Sprite spark = new Sprite(Texture.GetTexture("zmh1zo.png"));
            spark.Scale = 1f;
            spark.Layer = -0.95f;
            spark.Superbright = false;

            //spark.Color.G = 0;
            Random rnd = new Random();
            spark.Color.R = rnd.Next(2);
            spark.Color.G = rnd.Next(2);
            spark.Color.B = rnd.Next(2);

            if (spark.Color.R == 0f && spark.Color.G == 0f)
            {
                spark.Color.R = 1f;
                spark.Color.G = 1f;
            }

            spark.X = source.Sprite.X + source.Sprite.Width / 2 - spark.Width / 2;
            spark.Y = source.Sprite.Y + source.Sprite.Height / 2 - spark.Height / 2;

            // TODO: restore original ignoreglobalcolor state!!
            source.Sprite.IgnoreGlobalColor = true;

            // Wait for duration
            Ticker.AddTimer(delegate(float ratio, float deltaratio)
            {
                spark.Scale -= deltaratio;
                spark.X = source.Sprite.X + source.Sprite.Width / 2 - spark.Width / 2;
                spark.Y = source.Sprite.Y + source.Sprite.Height / 2 - spark.Height / 2;
                spark.Color.A -= deltaratio * 2;
                Graphics.GlobalColor.B -= deltaratio * 2;
                Graphics.GlobalColor.G -= deltaratio;
                Graphics.GlobalColor.R -= deltaratio * 0.4f;
            }, 800);

            // Easiest way to clean up a sprite, heh.
            sourcePowa.Dispose();

            source.Sprite.IgnoreGlobalColor = false;

            Graphics.GlobalColor.R = 1f;
            Graphics.GlobalColor.B = 1f;
            Graphics.GlobalColor.G = 1f;

            for (int i = 0; i < targets.Length; i++)
            {
                sprites[i].Playing = true;
                sprites[i].Visible = true;
            }

            Sprite fireback = new Sprite(Texture.GetTexture("DIRTYvmsff.jpg"));
            //Sprite fireback = new Sprite(Texture.GetTexture("nogoMYEYES.png")); //"nogoy.png"));
            //fireback.Rotation = (float)(rnd.NextDouble() * 40f - 20f);
            //fireback.Scale = 1 + 1 * (Math.Abs(fireback.Rotation) / 40f);
            fireback.Layer = -0.5f;

            // Wait for duration
            Ticker.AddTimer(delegate(float ratio, float deltaratio)
            {
                //ratio = ratio - 0.9f;
                //if (ratio < 0)
                //    ratio = 0;
                //ratio *= 10f;
                fireback.Color.A = 1 - ratio;
                fireback.Y -= (int)(deltaratio * 100);
            }, 1500);

            // Cleanup
            for (int i = 0; i < sprites.Length; i++)
                sprites[i].Dispose();
            fireback.Dispose();
            spark.Dispose();
        }
    }
}
